//
//  ITLoginViewController.swift
//  InternalTool-Swift
//
//  Created by Daniel Hu on 12/5/16.
//  Copyright © 2016 com.mask. All rights reserved.
//

import UIKit
import SnapKit
import Alamofire
import SVProgressHUD
import Foundation

class LoginViewController: UIViewController, UITextFieldDelegate {
    
    // MARK: - Properties
    
    lazy var companyName: UILabel = {
        let label = UILabel()
        label.text = "四川蓝瑞科技有限公司"
        label.font = UIFont.systemFont(ofSize: 20)
        label.textColor = .white
        label.textAlignment = .center
        return label
    }()
    
    lazy var accountTF: UITextField = {
        let accountTF = UITextField.textFieldWith(placeholder: "请输入您的工作号", leftViewImageName: "avatar")
        accountTF.keyboardType = UIKeyboardType.alphabet
//        accountTF.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
        return accountTF
    }()
    
    lazy var passwordTF: UITextField = {
        let passwordTF = UITextField.textFieldWith(placeholder: "请输入密码", leftViewImageName: "lock")
        passwordTF.isSecureTextEntry = true
        passwordTF.keyboardType = UIKeyboardType.alphabet
//        passwordTF.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
        return passwordTF
    }()
    
    lazy var loginButton: UIButton = {
        let loginBtn = UIButton.loginButton()
        loginBtn.addTarget(self, action: #selector(loginButtonDidPressed), for: UIControlEvents.touchUpInside)
        return loginBtn
    }()
    
    // MARK: - ViewController liftcycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUserInterface()
    }
    
    // MARK: - Userinterface
    
    func setupUserInterface() -> Void {
        view.backgroundColor = Color.bg
        
        view.addSubview(loginButton)
        loginButton.snp.makeConstraints { make in
            make.center.equalTo(view);
            make.left.equalTo(view).offset(23);
            make.right.equalTo(view).offset(-23);
            make.height.equalTo(50);
        }
        
        view.addSubview(passwordTF)
        passwordTF.snp.makeConstraints { make in
            make.bottom.equalTo(loginButton.snp.top).offset(-40);
            make.left.equalTo(view).offset(20);
            make.right.equalTo(view).offset(-20);
            make.height.equalTo(50);
        }
        
        view.addSubview(accountTF)
        accountTF.snp.makeConstraints { make in
            make.bottom.equalTo(passwordTF.snp.top).offset(-1);
            make.left.equalTo(view).offset(20);
            make.right.equalTo(view).offset(-20);
            make.height.equalTo(50);
        }
        
        view.addSubview(companyName)
        companyName.snp.makeConstraints { make in
            make.bottom.equalTo(accountTF.snp.top).offset(-10)
            make.left.right.equalTo(view)
            make.height.equalTo(30)
        }
    }
    
    // MARK: - Event response
    
    func loginButtonDidPressed() -> Void {
        
        SVProgressHUD.show()
        view.endEditing(true)
        
        guard let username = accountTF.text, username.characters.count > 0 else {
            SVProgressHUD.showInfo(withStatus: "用户名不能为空")
            return
        }
        guard let password = passwordTF.text, password.characters.count > 0 else {
            SVProgressHUD.showInfo(withStatus: "密码不能为空")
            return
        }
        
        HTTPRequest.authWith(userName: username, password: password) { (name, token) in
            let user = User.shared
            user.token = token
            user.name = name
            user.archive()
            
            NotificationCenter.default.post(name: .userDidLogedin, object: nil)
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
//    func textFieldDidChange() -> Void {
//        if let account = accountTF.text, let password = passwordTF.text {
//            if !account.isEmpty && !password.isEmpty {
//                loginButton.backgroundColor = Color.bg
//                loginButton.isEnabled = true
//            } else {
//                loginButton.backgroundColor = Color.bgGray
//                loginButton.isEnabled = false
//            }
//        }
//    }
}
